﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MxitAPI;

namespace MxitGUI {
    public partial class ChatWindowContainer : Form {

        //private int numChatWindows;
        //private List<ChatWindow> chatWindows;
        private Dictionary<ChatSession, ChatControl> chatWindows;

        //private ChatSessions chatSessions;
        private Contacts contacts;

        private bool quit;

        public ChatWindowContainer( ChatSessions chatSessions, Contacts contacts ) {
            InitializeComponent();

            quit = false;
            this.contacts = contacts;
            chatWindows = new Dictionary<ChatSession, ChatControl>();

            chatSessions.NewMessagesReceived += new ChatSessionsEventHandler( HandleNewMessagesReceived );
            chatSessions.ChatSessionCreated += new ChatSessionsEventHandler( HandleNewChatSessionCreated );

        }

        private void HandleNewChatSessionCreated( ChatSessions chatSessions, AffectedChatSessionsEventArgs e ) {

            if ( InvokeRequired ) {
                Invoke( new ChatSessionsEventHandler( HandleNewChatSessionCreated ), chatSessions, e );
                return;
            }

            foreach ( ChatSession chatSession in e.AffectedChatSession ) {
                // Create new chat windows if the chat window does not exist
                if ( !chatWindows.ContainsKey( chatSession ) ) {
                    /*ChatControl chatControl = new ChatControl( chatSession, contacts );
                    chatControl.Show();
                    //chatControl.MdiParent = this;
                    TabPage tabPage = new TabPage();
                    tabPage.Parent = tabControl;
                    tabPage.Text = chatControl.ChatControlText;
                    tabPage.Show();
                    chatControl.TabControl = tabControl;
                    //tabPage.
                   // chatWindow.MdiParent = tabPage;
                    tabControl.Controls.Add( tabPage );
                    tabPage.Controls.Add( chatControl );
                    chatControl.TabPage = tabPage;
                    chatControl.Show();
                    chatWindows.Add( chatSession, chatControl );
                    tabControl.SelectedTab = tabPage;*/
                 
                    ChatControl chatControl = new ChatControl( chatSession, contacts );
                    chatControl.Dock = DockStyle.Fill;
                    TabPage tabPage = new TabPage();
                    tabControl.TabPages.Add( tabPage );
                    tabPage.Controls.Add( chatControl );
                    tabPage.Text = chatControl.Text;
                    tabControl.SelectedTab = tabPage;
                    chatControl.TabPage = tabPage;
                    chatControl.TabControl = tabControl;

                    //newpage.Location = new System.Drawing.Point( 4, 26 );
                    //newpage.Name = "AddMaintAgreement" ;
                    //newpage.Size = new System.Drawing.Size( 736, 523 );
                   // newpage.TabIndex = 10;
                    //newpage.Text = "Add Maintenance Agreement";
                    //newpage.UseVisualStyleBackColor = true;

                }
            }
            Show();
            this.BringToFront();
        }

        private void HandleNewMessagesReceived( ChatSessions chatSessions, AffectedChatSessionsEventArgs e ) {

            if ( InvokeRequired ) {
                Invoke( new ChatSessionsEventHandler( HandleNewMessagesReceived ), chatSessions, e );
                return;
            }

            foreach ( ChatSession chatSession in e.AffectedChatSession ) {
                // Handle messages
            }

        }

        public void Quit() {
            quit = true;
        }

        private void ChatWindowContainer_FormClosing( object sender, FormClosingEventArgs e ) {
            Hide();
            if ( !quit )
                e.Cancel = true;
            
        }
        /*
        private void tabControl_SelectedIndexChanged( object sender, EventArgs e ) {
            foreach ( ChatWindow chatWindow in this.MdiChildren ) {
                if ( chatWindow.TabPage.Equals( tabControl.SelectedTab ) ) {
                    chatWindow.Select();
                }
            }
        }*/


    }
}
